var app = getApp()
Page({
  data: {
    banners: [],
    index: 0,
    unitPrice: 0,
    totalPrice: 0,
    purchaseCount: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    productInfo: [],
    productId: null,
    productItems: [],
    size: '',
    idSelect: 0
  },
  onLoad: function (options) {
    var _this = this;
    wx.request({
      url: 'https://cake.cindata.cn/product/getProductInfoById',
      data: {
        productId: options.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      complete: function complete(res) {
        if (res.statusCode == 200) {
          // 设置默认选择尺寸的背景颜色
          var idSelectTemp = 0
          if (res.data.productItems.length > 0) {
            for (var i = 0; i < res.data.productItems.length; i++) {
              if (res.data.productItems[i].id == res.data.productItem.id) {
                idSelectTemp = res.data.productItems[i].id
              }
            }
            _this.setData({
              size: res.data.productItem.size
            })
          }

          _this.setData({
            productInfo: res.data,
            unitPrice: res.data.price,
            totalPrice: res.data.price,
            productId: res.data.id,
            banners: res.data.attachments,
            productItems: res.data.productItems,
            idSelect: idSelectTemp
          })
        } else {
          console.log("请求商品详情失败请稍后重试！");
        }
      }
    })
  },
  quickBuy: function () {
    wx.navigateTo({
      url: '/pages/payment/payment?index=' + this.data.index + '&totalPrice=' + this.data.totalPrice + '&id=' + this.data.productId + '&unitPrice=' + this.data.unitPrice + '&size=' + this.data.size
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      totalPrice: this.data.unitPrice * this.data.purchaseCount[e.detail.value]
    })
  },
  changeProductSize: function (e) {
    console.log(e);
    this.setData({
      unitPrice: e.currentTarget.dataset.price,
      totalPrice: e.currentTarget.dataset.price,      
      size: e.currentTarget.dataset.size,
      index: 0,
      idSelect: e.currentTarget.dataset.id
    })
  },
})